import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutterroom/config.dart';
import 'package:flutterroom/pages/my/widget/common_check_button.dart';

class RoomApplianceItem {
  final String title;
  final int iconPoint;
  final bool isChecked;

  const RoomApplianceItem(this.title, this.iconPoint, this.isChecked);
}

const List<RoomApplianceItem> _dataList = [
  RoomApplianceItem('衣柜', 0xe918, false),
  RoomApplianceItem('洗衣机', 0xe917, false),
  RoomApplianceItem('空调', 0xe90d, false),
  RoomApplianceItem('天然气', 0xe90f, false),
  RoomApplianceItem('冰箱', 0xe907, false),
  RoomApplianceItem('暖气', 0xe910, false),
  RoomApplianceItem('电视', 0xe908, false),
  RoomApplianceItem('热水器', 0xe912, false),
  RoomApplianceItem('宽带', 0xe90e, false),
  RoomApplianceItem('沙发', 0xe913, false),
];

// _RoomApplianceState createState() => _RoomApplianceState();

class RoomAppliance extends StatefulWidget {
  final ValueChanged<List<RoomApplianceItem>> onChanged;

  const RoomAppliance({Key key, this.onChanged}) : super(key: key);
  @override
  _RoomApplianceState createState() => _RoomApplianceState();
}

class _RoomApplianceState extends State<RoomAppliance> {
  List<RoomApplianceItem> list = _dataList;
  @override
  Widget build(BuildContext context) {
    return Container(
        child: Wrap(
      runSpacing: 30,
      children: list
          .map((item) => GestureDetector(
                behavior: HitTestBehavior.translucent,
                onTap: () {
                  setState(() {
                    list = list
                        .map((items) => items == item
                            ? RoomApplianceItem(
                                item.title, item.iconPoint, !item.isChecked)
                            : items)
                        .toList();
                  });
                  if (widget.onChanged != null) {
                    widget.onChanged(list);
                  }
                  // RoomApplianceItem items =list.map() as RoomApplianceItem;
                },
                child: Container(
                  width: ScreenUtil().setWidth(750 - 1) / 5,
                  child: Column(
                    children: <Widget>[
                      Icon(
                        IconData(item.iconPoint, fontFamily: Config.CommonIcon),
                        size: 40,
                      ),
                      Padding(
                        padding: const EdgeInsets.all(10.0),
                        child: Text(item.title),
                      ),
                      CommonCheckButton(item.isChecked),
                    ],
                  ),
                ),
              ))
          .toList(),
    ));
  }
}

class RoomDetailApplicance extends StatelessWidget {
  final List<String> list;

  const RoomDetailApplicance({Key key, this.list}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    //去有相等元素
    // List<RoomApplianceItem> shows =List();
    // for (RoomApplianceItem title in _dataList) {
    //   for (String titles in list) {
    //     if (titles == title.title) {
    //       shows.add(title);
    //     }
    //   }
    // }
    // print("${shows[0].title}");
    var showList =
        _dataList.where((element) => list.contains(element.title)).toList();
    if (showList.length == 0) {
      return Container(
        padding: EdgeInsets.only(left: 10),
        child: Text("暂无房屋配置信息"),
      );
    }
    return Container(
        child: Wrap(
      runSpacing: 30,
      children: showList
          .map((item) => Container(
                width: ScreenUtil().setWidth(750 - 1) / 5,
                child: Column(
                  children: <Widget>[
                    Icon(
                      IconData(item.iconPoint, fontFamily: Config.CommonIcon),
                      size: 40,
                    ),
                    Padding(
                      padding: const EdgeInsets.all(10.0),
                      child: Text(item.title),
                    ),
                  ],
                ),
              ))
          .toList(),
    ));
  }
}
